/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;

import javax.annotation.PostConstruct;

import org.jefb.service.IUserSessionService;
import org.jefb.web.model.MessagesModel;
import org.jefb.web.model.entity.Message;
import org.jefb.web.model.entity.MessageType;
import org.jefb.web.util.ZKUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Timer;
import org.zkoss.zul.Window;

@org.springframework.stereotype.Component
@Scope("prototype")
public class MessagesController extends GenericForwardComposer implements
		Serializable {

	private static final long serialVersionUID = 1L;
	// Models
	@Autowired
	private IUserSessionService userSessionService;
	@Autowired
	private MessagesModel messagesModel;
	
	// UI components
	protected Window messagesWindow;
	protected Image messageIcon;
	protected Label messageLabel;
	protected Timer timer;

	public MessagesController() {

	}

	@PostConstruct
	public void init() {

	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		messagesModel.addPropertyChangeListener(MessagesModel.CURRENT_MESSAGE_PROPERTY, 
				new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				messagesWindow.setVisible(true);
				timer.setRunning(true);
				Message currentMessage = messagesModel.getCurrentMessage();
				if(currentMessage!=null){
					if(currentMessage.getType()!=null){
						MessageType type = currentMessage.getType();
						messageIcon.setSrc(type.getIconSrc());
					}
					String label = ZKUtils.getLabel(currentMessage.getTextKey());
					messageLabel.setValue(label);
				}
			}
		});
	}

	public void setMessagesModel(MessagesModel messagesModel) {
		this.messagesModel = messagesModel;
	}

	public MessagesModel getMessagesModel() {
		return messagesModel;
	}

	public void setUserSession(IUserSessionService userSessionService) {
		this.userSessionService = userSessionService;
	}

	public IUserSessionService getUserSession() {
		return userSessionService;
	}

}
